package com.tars.txtm.aspect;

import com.tars.txtm.connection.MyConnection;
import com.tars.txtm.trasactional.MyTransactionalManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;

/**
 * @author: Franz 2022/3/19
 */
@Aspect
@Component
public class MyConnectionAspect {

    @Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Connection invoke(ProceedingJoinPoint joinPoint){
        try {
            Connection conn = (Connection)joinPoint.proceed();
            String txGroupID = MyTransactionalManager.getCurrentTxGroupID();
            return new MyConnection(conn, MyTransactionalManager.getCurrentThreadTX(txGroupID));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
